<?php
/**
 * return the current microtime
 *
 * @return float
 */
function get_microtime_float()
{
    list( $usec, $sec ) = explode( ' ', microtime() );
    return ( (float)$sec + (float)$usec );
}

/**
 * write the debug-log
 *
 * @param string $value
 * @param string $file
 * @param integer $line
 * @param mixed $args
 * @param integer $level
 */
require_once( 'Log.php' );
function debug( $value, $file, $line, $args = null, $level = 'DEBUG' )
{
    // log file
    //
    $log_file = LOG_FILE;

    // log level
    //
    if( true !== defined( 'PEAR_LOG_' . LOG_LEVEL ) )
    {
        $log_level = PEAR_LOG_CRIT;
    }
    else
    {
        $log_level = constant( 'PEAR_LOG_' . LOG_LEVEL );
    }

    // log configuration
    //
    $log_conf = array( 'mode' => 0644, 'timeFormat' => '%Y/%m/%d %H:%M:%S' );

    // log handler
    //
    $handler = 'file';
    if( true !== is_writeable( dirname( $log_file ) ) )
    {
        $handler = 'error_log';
    }

    // write log level
    //
    if( true !== defined( 'PEAR_LOG_' . $level ) )
    {
        $level = PEAR_LOG_DEBUG;
    }
    else
    {
        $level = constant( 'PEAR_LOG_' . $level );
    }

    // write log
    //
    $message = '(' . getmypid() . ') ' . $value . ' - ' . $file . ' - ' . $line;
    if( true !== empty( $args ) )
    {
        $message .= "\n";
        $message .= var_export( (array)$args, true );
    }
    $logger =& Log::singleton( $handler, $log_file, 'ident', $log_conf, $log_level );
    $logger->log( $message, $level );
}

function is_secure_path( $public_dir, $path )
{
    if( true !== is_string( $path ) )
    {
        return false;
    }

    $realpath = realpath( $path );

    if( false === $realpath )
    {
        return false;
    }

    if( false === file_exists( $realpath ) )
    {
        return false;
    }

    if( 0 !== strncmp( $public_dir, $realpath, strlen( $public_dir ) ) )
    {
        return false;
    }

    return true;
}
